package me.sealer.ssm.controller;

import me.sealer.ssm.constant.ChinaAreaLevel;
import me.sealer.ssm.constant.Dict;
import me.sealer.ssm.constant.PageConstant;
import me.sealer.ssm.constant.PermissionConstant;
import me.sealer.ssm.model.ChinaArea;
import me.sealer.ssm.page.PageResult;
import me.sealer.ssm.service.ChinaAreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 功能描述： $ ChinaAreaController
 *
 * @author sealer
 * @email 1178884049@qq.com
 * @date 2019年11月15日 14时42分05秒
 */
@Controller
@RequestMapping("/chinaAreaOperations")
@RequiresPermissions(value = {PermissionConstant.AREA_MGMT})
public class ChinaAreaController {
    private final ChinaAreaService chinaAreaService;

    public ChinaAreaController(ChinaAreaService chinaAreaService) {
        this.chinaAreaService = chinaAreaService;
    }

    @RequestMapping("/queryArea")
    public ModelAndView queryArea(String name, Integer level, Integer zipCode, String cityCode, Long code, Long parentCode, Integer pageNo, Integer pageSize) {
        PageResult<ChinaArea> pageResult = chinaAreaService.queryAreaList(name, level, zipCode, cityCode, code, parentCode, pageNo, pageSize);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject(PageConstant.PAGE_RESULT, pageResult);
        modelAndView.setViewName("chinaAreaListResult");

        return modelAndView;
    }

    @RequestMapping("/queryAreaPre")
    public String queryAreaPre() {
        return "chinaAreaList";
    }

    @RequestMapping("/chinaAreaChangeLinked")
    public ModelAndView testChinaArea() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("chinaAreaChangeLinked");

        List<ChinaArea> chinaAreaList = chinaAreaService.queryAreas(ChinaAreaLevel.PROVINCE);
        modelAndView.addObject(Dict.PROVINCES, chinaAreaList);
        return modelAndView;
    }

    @RequestMapping("/city")
    @ResponseBody
    public List<ChinaArea> queryCities(@RequestParam("cityCode") Long code) {
        List<ChinaArea> chinaAreaList = chinaAreaService.queryAreas(ChinaAreaLevel.CITY, code);
        return chinaAreaList;
    }

    @RequestMapping("/county")
    @ResponseBody
    public List<ChinaArea> queryCounties(@RequestParam("countyCode") Long code) {
        List<ChinaArea> chinaAreaList = chinaAreaService.queryAreas(ChinaAreaLevel.COUNTY, code);
        return chinaAreaList;
    }

    @RequestMapping("/town")
    @ResponseBody
    public List<ChinaArea> queryTowns(@RequestParam("townCode") Long code) {
        List<ChinaArea> chinaAreaList = chinaAreaService.queryAreas(ChinaAreaLevel.TOWN, code);
        return chinaAreaList;
    }

    @RequestMapping("/village")
    @ResponseBody
    public List<ChinaArea> queryVillages(@RequestParam("villageCode") Long code) {
        List<ChinaArea> chinaAreaList = chinaAreaService.queryAreas(ChinaAreaLevel.VILLAGE, code);
        return chinaAreaList;
    }
}
